#include"ContactsServer.h"
using std::cout;
using std::endl;
using std::cerr;
using namespace httplib;
int main()
{
    cout<<"-----服务启动-----"<<endl;
    Server srv;//创建服务器对象
    ContactsServer contactsServer;
    srv.Post("/contacts/add",[contactsServer](const Request& req,Response& resp){
        add_req::AddContactRequest request;
        add_resp::AddContactResponse response;
        try{
            //反序列化
            if(!request.ParseFromString(req.body))
            {
                throw ContactExpection("Parse AddContactRequest error!");
            }
            contactsServer.add(request,&response);
            //序列化
            std::string resp_str;
            if(!response.SerializeToString(&resp_str))
            {
                throw ContactExpection("Serialize AddContactResponse error!");
            }
            resp.body = resp_str;
            resp.set_header("Content-Type","application/protobuf");
            resp.status = 200;
        }
        catch(ContactExpection& e)
        {
            cerr << "---> /contacts/add 发现异常!!!" << endl
                << "---> 异常信息：" << e.what() << endl;
            resp.status = 500;
            base_response::BaseResponse* baseResponse = response.mutable_base_resp();
            baseResponse->set_success(false);
            baseResponse->set_error_desc(e.what());
            std::string response_str;
            if (response.SerializeToString(&response_str)) {
              resp.body = response_str;
              resp.set_header("Content-Type", "application/protobuf");
            }   
        }
        catch(...)
        {
            cerr<<"未知错误！！！"<<endl;
        }     
    });
    srv.Post("/contacts/del", [contactsServer](const Request& req, Response& res) {
        del_req::DelContactRequest request;
        del_resp::DelContactResponse response;
        try {
            // 反序列化 request
            if (!request.ParseFromString(req.body)) {
                throw ContactExpection("Parse DelContactRequest error!");
            }
            // 删除联系人
            contactsServer.del(request, &response);
            // 序列化 response
            std::string response_str;
            if (!response.SerializeToString(&response_str)) {
                throw ContactExpection("Serialize DelContactResponse error");
            }
            res.body = response_str;
            res.set_header("Content-Type", "application/protobuf");
            res.status = 200;
        } catch (ContactExpection &e) {
            cerr << "---> /contacts/del 发现异常!!!" << endl
                << "---> 异常信息：" << e.what() << endl;
            res.status = 500;
            base_response::BaseResponse* baseResponse = response.mutable_base_resp();
            baseResponse->set_success(false);
            baseResponse->set_error_desc(e.what());
            std::string response_str;
            if (response.SerializeToString(&response_str)) {
              res.body = response_str;
              res.set_header("Content-Type", "application/protobuf");
            }
        }
        catch(...)
        {
            cerr<<"未知错误！！！"<<endl;
        }  
    });	

    srv.Post("/contacts/find-one", [contactsServer](const Request& req, Response& res) {
        find_one_req::FindOneRequest request;
        find_one_resp::FindOneResponse response;
        try {
            // 反序列化 request
            if (!request.ParseFromString(req.body)) {
                throw ContactExpection("Parse FindOneContactRequest error!");
            }
            // 查询联系人详细信息
            contactsServer.findOne(request, &response);
            // 序列化 response
            std::string response_str;
            if (!response.SerializeToString(&response_str)) {
                throw ContactExpection("Serialize FindOneContactResponse error");
            }
            res.body = response_str;
            res.set_header("Content-Type", "application/protobuf");
            res.status = 200;
        } catch (ContactExpection &e) {
            cerr << "---> /contacts/find-one 发现异常!!!" << endl
                << "---> 异常信息：" << e.what() << endl;
            res.status = 500;
            base_response::BaseResponse* baseResponse = response.mutable_base_resp();
            baseResponse->set_success(false);
            baseResponse->set_error_desc(e.what());
            std::string response_str;
            if (response.SerializeToString(&response_str)) {
              res.body = response_str;
              res.set_header("Content-Type", "application/protobuf");
            }
        }
        catch(...)
        {
            cerr<<"未知错误！！！"<<endl;
        }  
    });	

    srv.Get("/contacts/find-all", [contactsServer](const Request& req, Response& res) {
        find_all_resp::FindAllResponse response;
        try {
            // 查询所有联系人
            contactsServer.findAll(&response);
            // 序列化 response
            std::string response_str;
            if (!response.SerializeToString(&response_str)) {
                throw ContactExpection("Serialize FindAllContactsResponse error");
            }
            res.body = response_str;
            res.set_header("Content-Type", "application/protobuf");
            res.status = 200;
        } catch (ContactExpection &e) {
            cerr << "---> /contacts/find-all 发现异常!!!" << endl
                << "---> 异常信息：" << e.what() << endl;
            res.status = 500;
            base_response::BaseResponse* baseResponse = response.mutable_base_resp();
            baseResponse->set_success(false);
            baseResponse->set_error_desc(e.what());
            std::string response_str;
            if (response.SerializeToString(&response_str)) {
              res.body = response_str;
              res.set_header("Content-Type", "application/protobuf");
            }
        }
         catch(...)
        {
            cerr<<"未知错误！！！"<<endl;
        }  
    });

    srv.listen("0.0.0.0", 8123); 
    return 0;
}